package com.example.LibrarySystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.LibrarySystem.Exception.ServiceException;
import com.example.LibrarySystem.annotation.RoleAnnotation;
import com.example.LibrarySystem.mapper.BookMapper;
import com.example.LibrarySystem.mapper.InventoryMapper;
import com.example.LibrarySystem.pojo.Book;
import com.example.LibrarySystem.pojo.Inventory;
import com.example.LibrarySystem.pojo.User;
import com.example.LibrarySystem.pojo.dto.BookDto;
import com.example.LibrarySystem.response.Result;
import com.example.LibrarySystem.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/books")
@Transactional
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 添加
     *
     * @param book
     * @return
     */
    @PostMapping("/insert/{number}")
    @RoleAnnotation(type = "admin")
    public Result insert(@RequestBody Book book, @PathVariable Integer number) {
        try {
            int insert = bookService.insert(book);
            Integer id = book.getId();

            /**
             * 往库存添加数量
             */
            inventoryMapper.insert(new Inventory(null, id, number));
            return new Result("添加成功", 200);
        } catch (Exception e) {
            throw new ServiceException("-1", "添加失败");
        }
    }

    @PostMapping("/update/{number}")
    @RoleAnnotation(type = "admin")
    public Result update(@RequestBody Book book, @PathVariable Integer number) {
        int update = bookService.update(book, number);
        return update > 0 ? new Result("修改成功", 200) : new Result("修改失败", -1);
    }

    /**
     * 根据id 删除
     */
    @GetMapping("/delete/{id}")
    @RoleAnnotation(type = "admin")
    public Result deleteById(@PathVariable Integer id) {
        int res = bookMapper.delById(id);
        if (res > 0) {
            return new Result("删除成功", 200);
        }
        return new Result("删除失败请重试", -1);
    }

    /**
     * 条件查询所有书本
     */
    @GetMapping("/getBooksByAll")
    public Result getBookByAll(@RequestParam Map<String, String> data) {
        Result result = new Result();
        IPage<Book> BooksByAll = bookService.getBookByAll(data);
        List<Book> records = BooksByAll.getRecords();

        // 将Book 转换为BookDto
        List<BookDto> list = new ArrayList<>();
        for (Book book : records) {
            int bookId = book.getId();
            QueryWrapper<Inventory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("book_id", bookId);
            Inventory inventory = inventoryMapper.selectOne(queryWrapper);

            BookDto bookDto = BookDto.BookChangeBookDto(book, inventory.getNumber());
            list.add(bookDto);
        }

        result.put("data", list);
        result.put("total", BooksByAll.getTotal());
        result.setMsg("查询成功");
        result.setStatus(200);
        return result;
    }
}
